package lib;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;

public class CellInLib 
{
	private Cell m_cell;
	private boolean m_isEmpty;
	
	public CellInLib(Cell cell)
	{
		m_cell = cell;
		m_isEmpty = m_cell == null;
	}
	
	public boolean IsEmpty()
	{
		return m_isEmpty;
	}
	
	protected Cell getRealCell()
	{
		return m_cell;
	}
	
	public String getValue()
	{
		if(m_isEmpty)
		{
			return "";
		}
		switch (m_cell.getCellType()) 
		{
		case HSSFCell.CELL_TYPE_BLANK:
			return "";
		case HSSFCell.CELL_TYPE_BOOLEAN:
			return "" + m_cell.getBooleanCellValue();
		case HSSFCell.CELL_TYPE_NUMERIC:
		{
			double value = m_cell.getNumericCellValue();
			if(value == (int)value)
			{
				return (int)value + "";
			}
			return value + "";
		}
		case HSSFCell.CELL_TYPE_STRING:
			return m_cell.getStringCellValue();
		case HSSFCell.CELL_TYPE_FORMULA:
			return Formula.getValue(this);
		default:
			return "";
		}
	}
	
	public void setValue(String value)
	{
		m_cell.setCellValue(value);
	}
	
	public void setColor(short color, CellStyle style)
	{
		style.setFillBackgroundColor(color);
		style.setFillForegroundColor(color);
		style.setFillPattern(CellStyle.SOLID_FOREGROUND);
		m_cell.setCellStyle(style);
	}
}
